package cn.iocoder.yudao.module.dt.service.wxnews;

import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;

import java.util.*;
import cn.iocoder.yudao.module.dt.controller.admin.wxnews.vo.*;
import cn.iocoder.yudao.module.dt.dal.dataobject.wxnews.WxNewsDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;

import cn.iocoder.yudao.module.dt.convert.wxnews.WxNewsConvert;
import cn.iocoder.yudao.module.dt.dal.mysql.wxnews.WxNewsMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.dt.enums.ErrorCodeConstants.*;

/**
 * 新闻 Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
public class WxNewsServiceImpl implements WxNewsService {

    @Resource
    private WxNewsMapper wxNewsMapper;

    @Override
    public Integer createWxNews(WxNewsCreateReqVO createReqVO) {
        // 插入
        WxNewsDO wxNews = WxNewsConvert.INSTANCE.convert(createReqVO);
        wxNewsMapper.insert(wxNews);
        // 返回
        return wxNews.getId();
    }

    @Override
    public void updateWxNews(WxNewsUpdateReqVO updateReqVO) {
        // 校验存在
        this.validateWxNewsExists(updateReqVO.getId());
        // 更新
        WxNewsDO updateObj = WxNewsConvert.INSTANCE.convert(updateReqVO);
        wxNewsMapper.updateById(updateObj);
    }

    @Override
    public void deleteWxNews(Integer id) {
        // 校验存在
        this.validateWxNewsExists(id);
        // 删除
        wxNewsMapper.deleteById(id);
    }

    private void validateWxNewsExists(Integer id) {
        if (wxNewsMapper.selectById(id) == null) {
            throw exception(WX_NEWS_NOT_EXISTS);
        }
    }

    @Override
    public WxNewsDO getWxNews(Integer id) {
        return wxNewsMapper.selectById(id);
    }

    @Override
    public List<WxNewsDO> getWxNewsList(Collection<Integer> ids) {
        return wxNewsMapper.selectBatchIds(ids);
    }

    @Override
    public PageResult<WxNewsDO> getWxNewsPage(WxNewsPageReqVO pageReqVO) {
        return wxNewsMapper.selectPage(pageReqVO);
    }

    @Override
    public List<WxNewsDO> getWxNewsList(WxNewsExportReqVO exportReqVO) {
        return wxNewsMapper.selectList(exportReqVO);
    }

}
